package com.xlh.openai.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.util.UUIDUtils;
import com.sun.org.apache.regexp.internal.RE;
import com.xlh.openai.common.Result;
import com.xlh.openai.exception.ServiceException;
import com.xlh.openai.param.ChatStreamQuestionParam;
import com.xlh.openai.service.ConversationService;
import com.xlh.openai.service.ExchangeCodeService;
import com.xlh.openai.service.WebChatService;
import com.xlh.openai.service.WechatUserService;
import com.xlh.openai.utils.RequestUtil;
import com.xlh.openai.vo.ConversationListVO;
import com.xlh.openai.vo.UserInfoVO;
import com.xlh.openai.vo.WechatQRcodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;


/**
 * @author chufangzheng
 */
@Api(tags = "网页版ChatGpt")
@RestController
@RequestMapping("web")
@Slf4j
public class WebController {

    @Autowired
    private WebChatService webChatService;

    @Autowired
    private ExchangeCodeService exchangeCodeService;


    @ApiOperation("获取二维码")
    @GetMapping(value = "createLoginUrl")
    public Result<WechatQRcodeVO> getQRcode(HttpSession session){
        WechatQRcodeVO qRcode = webChatService.getQRcode(session);
        return Result.success(qRcode);
    }


    @ApiOperation("轮询调用用户是否扫码登陆")
    @GetMapping(value = "tryLogin")
    public Result<UserInfoVO> tryLogin(HttpSession session, String sceneId){
        UserInfoVO userInfo = webChatService.wechatIsLogin(session, sceneId);
        return Result.success(userInfo);
    }


    @ApiOperation("兑换激活码")
    @GetMapping(value = "exchange")
    public Result<UserInfoVO> exchange(String code){
        exchangeCodeService.exchange(code);
        return Result.success();
    }

    @ApiOperation("生成激活码")
    @GetMapping(value = "getExchangeCode", produces = {"application/octet-stream"})
    public Result getExchangeCode( Integer changeNumber , String userName,
                                   String password, String cp, HttpServletRequest request){

        // 获取当前访问IP
        String ipAddress = RequestUtil.getIpAddress(request);
        if(userName.equals("chufangzheng") && password.equals("xlh82283322.") && cp.equals("xlh")){
            // 锁定只有我能够操作
            exchangeCodeService.getExchangeCode(changeNumber);
        }else{
            throw new ServiceException("用户名密码错误");
        }
        return Result.success();
    }

    @ApiOperation("chatGpt流问答")
    @PostMapping( path="question" )
    public SseEmitter chatStreamQuestion( @RequestBody @Valid ChatStreamQuestionParam chatStreamQuestionParam){
        String prompt = chatStreamQuestionParam.getPrompt();
        String messageId = chatStreamQuestionParam.getOptions().getParentMessageId();
        if(StrUtil.isBlank(prompt) && ObjectUtil.isNull(chatStreamQuestionParam.getTopicId())){
            throw new ServiceException("提问信息不能为空！");
        }
        if( StrUtil.isBlank(chatStreamQuestionParam.getOptions().getConversationId()) ){
            chatStreamQuestionParam.getOptions().setConversationId(IdUtil.nanoId());
        }
        SseEmitter sseEmitter = webChatService.chatStreamQuestion(prompt,
                messageId, chatStreamQuestionParam.getOptions().getConversationId(), chatStreamQuestionParam.getTopicId());

        return sseEmitter;
    }

    @ApiOperation("testChatGpt流问答")
    @PostMapping( path="testQuestion" )
    public SseEmitter testChatStreamQuestion( @RequestBody @Valid ChatStreamQuestionParam chatStreamQuestionParam){
        String prompt = chatStreamQuestionParam.getPrompt();
        String messageId = chatStreamQuestionParam.getOptions().getParentMessageId();
        if(StrUtil.isBlank(prompt) && ObjectUtil.isNull(chatStreamQuestionParam.getTopicId())){
            throw new ServiceException("提问信息不能为空！");
        }
        if( StrUtil.isBlank(chatStreamQuestionParam.getOptions().getConversationId()) ){
            chatStreamQuestionParam.getOptions().setConversationId(IdUtil.nanoId());
        }
        SseEmitter sseEmitter = webChatService.testChatStreamQuestion(prompt,
                messageId, chatStreamQuestionParam.getOptions().getConversationId(), chatStreamQuestionParam.getTopicId());

        return sseEmitter;
    }



}
